import { v4 as uuidv4 } from 'uuid';
import types from "./mutation-types";
import menu from "../menu";
import config from '@/static/config.js';
import utils from '@/static/js/utils.js';
import { getCurrentUserApi } from '@/api/index.js';
/**
 * 通过meta.role判断是否与当前用户权限匹配
 * @param {array} permissionCode
 * @param {object} itemData 某个菜单对象
 */
const hasPermission = function (permissionCode, itemData) {
    if (itemData.authCode) {
        return permissionCode.some(ele => itemData.authCode===ele);
    } else {
        return true;
    }
  }
  
/**
 * 递归过滤，返回有权限的菜单数据
 * @param {array} menuData   菜单数据
 * @param {array} permissionCode  所有权限码
 */
const filteMenuData = function (menuData, permissionCode) {
    const result = menuData.filter(item => {
        if (hasPermission(permissionCode, item)) {
            item.index = item.link;
            if (item.children && item.children.length) {
                item.index = uuidv4().replace(/-/g, '');
                item.children = filteMenuData(item.children, permissionCode);
                if(!item.children.length) return false;
            }else if(item.children && !item.children.length) {
                return false;
            }
            return true;
        }
        return false;
    })
    return result;
}

/**
 * 权限编码处理
 * @param {object} data  请求数据aclMap
 */
const getPermissionCode = function (data) {
    let permissionCode = [];
    if(!data) return permissionCode;
    Object.keys(data).forEach((key) => {
        let item = data[key];
        for (let i = 0; i < item.length; i++) {
            let code = item[i];
            if (permissionCode.indexOf(code)===-1) permissionCode.push(code);
        }
    })
    return permissionCode;
}

const permission = {
    namespaced: true,
    state:{
        menuData: [],
        permissionCode: [],
        userInfo: {},
    },
    getters: {
        menuData: state => state.menuData,
        permissionCode: state => state.permissionCode,
        permissions: state => {
            return (code) => {
                let indexCode = state.permissionCode.indexOf(code);
                return indexCode!==-1 ? true : false;
            }
        },
        userInfo: state => state.userInfo,
    },
    mutations:{
        [types.SET_PERMISSION_CODE]: (state, data) => {
            state.permissionCode = data;
            utils.sessionStorage.set('permissionCode', data);
        },
        [types.SET_MENU_DATA]: (state, data) => {
            state.menuData = data;
            utils.sessionStorage.set('menuData', data);
        },
        [types.SET_USER_INFO]: (state, data) => {
            state.userInfo = data;
            utils.sessionStorage.set('userInfo', data);
        },
    },
    actions:{
        // 设置权限编码
        setPermissionCode({ commit }, data) {
            let authCodes = data || [];
            commit(types.SET_PERMISSION_CODE, authCodes);
        },
        // 设置权限菜单
        setMenuData({ commit }, data) {
            let menuData = data || [];
            commit(types.SET_MENU_DATA, menuData);
        },
        getCurrentUserInfo({ dispatch, commit }, callback) {
            getCurrentUserApi().then(res => {
                // console.log(res.data.aclMap);
                let data = getPermissionCode(res.data.aclMap);
                commit(types.SET_PERMISSION_CODE, data);

                let menuData = filteMenuData(menu, data);
                commit(types.SET_MENU_DATA, menuData);
                
                commit(types.SET_USER_INFO, res.data);
                
                utils[config.tokenStorage].set('username', res.data.username);

                if(callback) callback(res);
            })
        },
    }
    
}


export default permission;